//#include "ev.h"
#include <inavr.h>
#include "uart.h"
#include "opcmd.h"
#include "config.h"

//#include "ds18b20.h"


#define TEST_ENABLE		0


#if(TEST_ENABLE)
void func1(u16 ev_id)
{
	switch(ev_id)
	{
		case EVENT_ID_TIMER_DEBUG:
			F(("mod1: %x \r\n",ev_id));
			break;
		default:
			break;
	}
	return;
}

void modu1_init(void)
{
	event_regist(func1);
}

#endif

#include "list.h"
#include "flash.h"
#include "alloc.h"
list_t g_sensor_list; 
#if 0 
void debug_sensor_list(void) 
{
	list_node_t* pnode;
	st_sensor_fdata* sensor;
	int cnt=0;
	for (pnode = list_begin(&g_sensor_list); pnode != list_end(&g_sensor_list);pnode = list_next(pnode))
	{
		sensor = entry_list(pnode, st_sensor_fdata, node);
		printf("cnt:%d(%p) x=%d y=%d addr=%d\n",cnt++,(char*)sensor,(int)sensor->funit.x_idx,(int)sensor->funit.y_idx,(int)sensor->funit.io_idx);

	}

	return;
}

int test_2(void)
{
	int i;
	u08*ptest;
	//u08*addr[15];
	st_sensor_fdata*psensor;
	//----------------------------------------
	st_sensor_fdata sen;
	memset(&sen,0xff,sizeof(sen));
	sen.funit.x_idx = 0;
	sen.funit.y_idx = 1;
	for(i = 2;i<10;i++)
		sen.funit.romid[i-2] = i;

	//----------------------------------------
	list_init(&g_sensor_list);

	user_malloc_init();

	debug_heap();
#if 0
	for(i = 0;i < 15;i++)
	{
		ptest = (u08*)user_malloc_alloc(13);
		if(ptest==0)
		{
			printf("ddddddddddddddddd:%d\n",i);
			break;
		}
		addr[i] = ptest;
		printf("%d alloc:%p\n",i,ptest);
	}

	debug_heap();
	user_malloc_free(addr[2]);
	user_malloc_free(addr[3]);
	user_malloc_free(addr[4]);
	debug_heap();

#endif

	ptest = (u08*)user_malloc_alloc(SENSOR_COUNTS_PER_PAGE*sizeof(st_sensor_fdata));
	if(ptest==NULL)
	{
		//printf("alloc for funit error\n");
		return -1;
	}

	debug_heap();

	mempool_sensor_fdata_init(ptest,SENSOR_COUNTS_PER_PAGE);
	//----------------------------------------

	for(i = 0;i<200;i++)
	{
		psensor = mempool_sensor_fdata_get();
		if(psensor==NULL)
		{
			//printf("%d mempool overflow!\n",i);
			break;
		}
		psensor->funit.x_idx = i;
		psensor->funit.y_idx = i*2;
		psensor->funit.io_idx = 0; 

		list_insert_back(&g_sensor_list,&psensor->node);

	}

	debug_sensor_list();

	//printf("page_size = %d\nsizeof(st_flash_unit) = %d\nsizeof(st_sensor_fdata) =%d\nsensor_cnt_per_page = %d\npage_remain = %d\n",
	//		FLASH_PAGE_SIZE,
	//		sizeof(st_flash_unit),
	//		sizeof(st_sensor_fdata),
	//		SENSOR_COUNTS_PER_PAGE,
	//		FLASH_PAGE_SIZE-SENSOR_COUNTS_PER_PAGE*sizeof(st_flash_unit));

	return 0;
}
#endif
#if 1
#define	CMD_OUT_LEN 64
u08 cmd_in[4];
//u08 cmd_out[CMD_OUT_LEN];
extern u08*	g_buff_handle_master ;

void test_master_cmd(u08 id,u08 para1,u08 para2)
{	
	s16 ret;
	u16 retlen = 0;
	u16 i;
	F(("-------------------\r\n"));
	F(("in : 0x%x %x %x\r\n",id,para1,para2));

	cmd_in[0] = id;
	cmd_in[1] = para1;
	cmd_in[2] = para2;


	ret = handle_master_cmd(cmd_in,NULL,&retlen);
	F(("out: %x\r\n",ret));
	for(i=0;i<retlen;i++)
	{
		if(!(i%16) && i)
		F(("\r\n"));
		F(("%d ",g_buff_handle_master[i]));
	}
	F(("\r\n"));
	
  return;   
}

#endif
void test_modu_func(void)
{
	u08 i = 0;
	while(1)
	{
		test_master_cmd(0x60,0xff,0xff);
		test_master_cmd(0x65,0xff,0xff);
		test_master_cmd(0x66,0xff,0xff);

		if(((i%10)==0)&&i)
			test_master_cmd(0x62,0xff,0xff);
		i++;
		delayms(1500);
	}
	//test_2();

	//u32 addr;
	//addr = 0x100*0x101;
	//printf("test_addr:%x\n",addr);
#if 0
#define CMD_OUT_LEN   80
	u08 cmd_in[4];
	u08 cmd_out[CMD_OUT_LEN];
	u08 i;
	u08 ret_len;
	/*  
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&  0x60
	F(("-------------------\r\n"));
	F(("in : 0x60 0 32\r\n"));
	cmd_in[0] = 0x60;
	cmd_in[1] = 0;
	cmd_in[2] = 32;
	for(i=0;i<CMD_OUT_LEN;i++)
	cmd_out[i] = 0;

	handle_master_cmd(cmd_in,cmd_out);
	F(("out:\r\n"));
	for(i=0;i<CMD_OUT_LEN;i++)
	{
	if(!(i%16) && i)
	F(("\r\n"));
	F(("%d ",cmd_out[i]));
	}
	F(("\r\n"));
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	F(("-------------------\r\n"));
	F(("in : 0x60 8 32\r\n"));
	cmd_in[0] = 0x60;
	cmd_in[1] = 8;
	cmd_in[2] = 32;
	for(i=0;i<CMD_OUT_LEN;i++)
	cmd_out[i] = 0;
	handle_master_cmd(cmd_in,cmd_out);
	F(("out:\r\n"));
	for(i=0;i<CMD_OUT_LEN;i++)
	{
	if(!(i%16) && i)
	F(("\r\n"));
	F(("%d ",cmd_out[i]));
	}  
	F(("\r\n"));

	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 0x62
	F(("-------------------\r\n"));
	F(("in : 0x62 0 32\r\n"));
	cmd_in[0] = 0x62;
	cmd_in[1] = 0;
	cmd_in[2] = 32;
	for(i=0;i<CMD_OUT_LEN;i++)
	cmd_out[i] = 0;
	handle_master_cmd(cmd_in,cmd_out);
	F(("out:\r\n"));
	for(i=0;i<CMD_OUT_LEN;i++)
	{
	if(!(i%16) && i)
	F(("\r\n"));
	F(("%d ",cmd_out[i]));
	}  
	F(("\r\n"));
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

	F(("-------------------\r\n"));
	F(("in : 0x62 8 32\r\n"));
	cmd_in[0] = 0x62;
	cmd_in[1] = 8;
	cmd_in[2] = 32;
	for(i=0;i<CMD_OUT_LEN;i++)
	cmd_out[i] = 0;
	handle_master_cmd(cmd_in,cmd_out);
	F(("out:\r\n"));
	for(i=0;i<CMD_OUT_LEN;i++)
	{
	if(!(i%16) && i)
	F(("\r\n"));
	F(("%d ",cmd_out[i]));
	}  
	F(("\r\n"));
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 
	F(("-------------------\r\n"));
	F(("in : 0x64 0 32\r\n"));
	cmd_in[0] = 0x64;
	cmd_in[1] = 0;
	cmd_in[2] = 8;
	for(i=0;i<CMD_OUT_LEN;i++)
		cmd_out[i] = 0;
	handle_master_cmd(cmd_in,cmd_out);
	F(("out:\r\n"));
	for(i=0;i<CMD_OUT_LEN;i++)
	{
		if(!(i%16) && i)
			F(("\r\n"));
		F(("%d ",cmd_out[i]));
	}  
	F(("\r\n"));
	*/ 
		//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
		/*
		   F(("-------------------\r\n"));
		   F(("in : 0x65 0 32\r\n"));
		   cmd_in[0] = 0x65;
		   cmd_in[1] = 0;
		   cmd_in[2] = 32;
		   for(i=0;i<CMD_OUT_LEN;i++)
		   cmd_out[i] = 0;
		   ret_len = handle_master_cmd(cmd_in,cmd_out);
		   F(("out:\r\n"));
		   for(i=0;i<ret_len;i++)
		   {
		   if(!(i%16) && i)
		   F(("\r\n"));
		   F(("%x ",cmd_out[i]));
		   }  
		   F(("\r\n"));
		   */
		//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
		/*
		   F(("-------------------\r\n"));
		   F(("in : 0x66 08 32\r\n"));
		   cmd_in[0] = 0x66;
		   cmd_in[1] = 8;
		   cmd_in[2] = 32;
		   for(i=0;i<CMD_OUT_LEN;i++)
		   cmd_out[i] = 0;
		   ret_len = handle_master_cmd(cmd_in,cmd_out);
		   F(("out:\r\n"));
		   for(i=0;i<ret_len;i++)
		   {
		   if(!(i%16) && i)
		   F(("\r\n"));
		   F(("%x ",cmd_out[i]));
		   }  
		   F(("\r\n"));
		   */
#endif

		return;
}





